/**
 * @Author: LongTao
 * @program: day04_longtao_springmvc_1917JA_20210108
 * @description: 用户实现类
 * @create: 2021-01-11 16:13
 * @version:1.0
 **/
package com.springmvc.longtao.smbms.service.impl;

import com.github.pagehelper.PageHelper;
import com.springmvc.longtao.smbms.dao.RoleMapper;
import com.springmvc.longtao.smbms.dao.UserAndRoleMapper;
import com.springmvc.longtao.smbms.dao.UserMapper;
import com.springmvc.longtao.smbms.pojo.Role;
import com.springmvc.longtao.smbms.pojo.User;
import com.springmvc.longtao.smbms.pojo.UserAndRole;
import com.springmvc.longtao.smbms.pojo.UserExample;
import com.springmvc.longtao.smbms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {
    /**
     * 注入userMapper接口
     */
    private UserMapper userMapper;
    @Autowired
    @Qualifier("userMapper")
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }



    /**
     * 注入userAndRoleMapper接口
     * @param userAndRoleMapper
     */
    private UserAndRoleMapper userAndRoleMapper;
    @Autowired
    @Qualifier("userAndRoleMapper")
    public void setUserAndRoleMapper(UserAndRoleMapper userAndRoleMapper) {
        this.userAndRoleMapper = userAndRoleMapper;
    }

    /**
     * 注入用户接口
     */
    private RoleMapper roleMapper;
    @Autowired
    @Qualifier("roleMapper")
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }
    /* private UserExample userExample;
    @Autowired
    @Qualifier("userExample")
    public void setUserExample(UserExample userExample) {
        this.userExample = userExample;
    }*/

    /**
     * 增加用户信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean add(User user) {
        boolean flag=false;
        if (userMapper.insert(user)>0){
            flag=true;
        }
        return flag;
    }

    /**
     * 用户登录
     *
     * @param userCode
     * @param userPassword
     * @return
     */
    @Override
    public User login(String userCode, String userPassword) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria =example.createCriteria();
        criteria.andUserCodeEqualTo(userCode);
        criteria.andUserPasswordEqualTo(userPassword);
        List<User> list= userMapper.selectByExample(example);
        System.out.println("list = " + list);
        User user=null;
        if (list.size()>0){
            user=list.get(0);
        }
        return user;
    }

    /**
     * 根据条件查询用户列表
     *
     * @param queryUserName
     * @param queryUserRole
     * @param currentPageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<UserAndRole> getUserList(String queryUserName, Integer queryUserRole, Integer currentPageNo, Integer pageSize) {
        /**
         * 实现分页效果
         */
        PageHelper.startPage(currentPageNo,pageSize);
        List<UserAndRole> list=userAndRoleMapper.queryAll(queryUserName, queryUserRole, currentPageNo, pageSize);
        return list;
    }

    /**
     * 根据条件查询用户表记录数
     *
     * @param queryUserName
     * @param queryUserRole
     * @return
     */
    @Override
    public int getUserCount(String queryUserName, Integer queryUserRole) {
         int count=userAndRoleMapper.getUserCount(queryUserName, queryUserRole);
        return count;
    }

    /**
     * 根据userCode查询出User
     *
     * @param userCode
     * @return
     */
    @Override
    public User selectUserCodeExist(String userCode) {
        return null;
    }

    /**
     * 根据ID删除user
     *
     * @param delId
     * @return
     */
    @Override
    public boolean deleteUserById(Integer delId) {
        boolean flag=false;
        int i=userMapper.deleteByPrimaryKey(Long.valueOf(delId));
        if (i>0){
            flag=true;
        }
        return flag;
    }

    /**
     * 根据ID查找user
     *
     * @param id
     * @return
     */
    @Override
    public User getUserById(String id) {
        User user=userMapper.selectByPrimaryKey(Long.valueOf(id));
        return user;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean modify(User user) {
        boolean flag=false;
        if (userMapper.updateByPrimaryKeySelective(user)>0){
            flag=true;
        }
        return flag;
    }

    /**
     * 根据userId修改密码
     *
     * @param id
     * @param pwd
     * @return
     */
    @Override
    public boolean updatePwd(int id, String pwd) {
        boolean flag=false;
        return flag;
    }

    /**
     * 查询角色表
     *
     * @return
     */
    @Override
    public List<Role> queryRole() {
        List<Role> list=roleMapper.selectByExample(null);
        return list;
    }

    /**
     * 查询用户具体信息
     *
     * @param id
     * @return
     */
    @Override
    public UserAndRole queryUserAndRoleID(String id) {
        UserAndRole userAndRole=userAndRoleMapper.queryID(id);
        return userAndRole;
    }

    /**
     * 根据code查询用户
     *
     * @param userCode
     * @return
     */
    @Override
    public UserAndRole queryUserCode(String userCode) {
        UserAndRole userAndRole=new UserAndRole();
        userAndRole=userAndRoleMapper.queryUserCode(userCode);
        return userAndRole;
    }
}